package com.yoyo.itravel.web.controller;

import com.yoyo.itravel.bean.PageBean;
import com.yoyo.itravel.bean.ResultInfo;
import com.yoyo.itravel.bean.User;
import com.yoyo.itravel.constants.UserConstant;
import com.yoyo.itravel.exceptions.UserException;
import com.yoyo.itravel.service.IUserService;
import com.yoyo.itravel.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Map;

/**
 * @author HZX
 * @Description 用户相关操作服务端
 * @date 2020/5/2 21:56
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private static final long serialVersionUID = 7966957510032142113L;

    @Autowired
    // 声明用户服务层接口对象
    private IUserService userService;

    /**
     * @param requset
     * @param response
     * @return void
     * @Description 注册时获得验证码（给手机发送验证码）
     */
    @RequestMapping("/sendVerifyCodeToPhone")
    protected void sendVerifyCodeToPhone(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
        // 获得手机号
        String phone = requset.getParameter("phone");
        // 发送短信，并获得产生的随机验证码
        String randomCode = ShortMsgUtils.sendShortMsg(phone);
        // 设置相应内容编码类型
        response.setContentType("text/html;charset=UTF-8");
        // 将验证码写回给客户端
        response.getWriter().write(randomCode);
    }

    /**
     * @param requset
     * @param response
     * @return void
     * @Description 注册时获得验证码（给邮箱发送验证码）
     */
    @RequestMapping("/sendVerifyCodeToEmail")
    protected void sendVerifyCodeToEmail(HttpServletRequest requset,
                                         HttpServletResponse response) throws ServletException, IOException {
        // 获得邮箱
        String email = requset.getParameter("email");
        // 生成随机验证码
        String randomCode = StringUtils.getRandomVerifyCode();
        // 给邮箱发送验证码
        MailUtils.senMail(email, "【爱旅游】验证码：" + randomCode + "，您正在申请注册<a href='http://localhost:8080/travel'>爱旅游</a>账号，需要进行验证，请勿泄露您的验证码！", "爱旅游账号注册验证码");
        // 设置相应内容编码类型
        response.setContentType("text/html;charset=UTF-8");
        // 将验证码写回给客户端
        response.getWriter().write(randomCode);
    }

    /**
     * @param requset
     * @param response
     * @return void
     * @Description 修改密码时获得验证码（给邮箱发送验证码）
     */
    @RequestMapping("/sendVerifyCodeToEmailUpdate")
    protected void sendVerifyCodeToEmailUpdate(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
        // 获得邮箱
        String email = requset.getParameter("email");
        // 生成随机验证码
        String randomCode = StringUtils.getRandomVerifyCode();
        // 给邮箱发送验证码
        MailUtils.senMail(email, "【爱旅游】验证码：" + randomCode + "，您正在修改<a href='http://localhost:8080/travel'>爱旅游</a>账号邮箱，需要进行验证，请勿泄露您的验证码！", "爱旅游账号注册验证码");
        // 设置相应内容编码类型
        response.setContentType("text/html;charset=UTF-8");
        // 将验证码写回给客户端
        response.getWriter().write(randomCode);
    }


    /**
     * @Description 用户注册
     *
     * @Param request
     * @Param response
     * @Return void
     */
    @RequestMapping("/register")
    protected void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得所有表单数据
        Map<String, String[]> parameterMap = request.getParameterMap();

        // 根据表单数据的Map集合封装User对象
        User user = BeanUtils.populate(User.class, parameterMap);
        // 构建返回前端数据对象
        ResultInfo info = new ResultInfo();

        try {
            // 执行用户注册操作
            userService.register(user);
            // 未出现异常，注册成功
            info.setFlag(true);
            //将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            // 出现异常，将异常信息添加至info中
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 用户登录
     */
    @RequestMapping("/login")
    protected void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得表单数据
        String phone_email = request.getParameter("phone_email");
        String password = request.getParameter("password");
        boolean isAutoLogin = request.getParameter("autoLogin") == null ? false : true;
        boolean isRememberPwd = request.getParameter("rememberPwd") == null ? false : true;

        // 获得Session对象
        HttpSession session = request.getSession();
        // 构建返回前端数据对象
        ResultInfo info = new ResultInfo();

        // 对密码进行MD5加密
//        password = MD5Utils.md5(password);

        try {
            // 进行用户登录操作
            User user = userService.login(phone_email, MD5Utils.md5(password));
            // 未出现异常，登录成功，在session对象中添加user对象
            session.setAttribute(UserConstant.USER, user);

            // 判断用户是否选择记住密码
            if (isRememberPwd) { // 选择记住密码
                // 构建保存用户登录信息的Cookie对象
                Cookie loginInfo = new Cookie(UserConstant.REMEMBER_LOGIN_INFO, phone_email + "&" + password + "&checked");
                loginInfo.setPath(request.getContextPath()); // 这里不设置一下路径发现最后的cookie总是保存后取不出来
                loginInfo.setMaxAge(UserConstant.REMEMBER_PWD_TIME); // 这里是按秒为单位的(设为两周内记住密码)
                response.addCookie(loginInfo);

            } else { // 未选择记住密码
                // 判断是否存在用户记住密码登录信息的cookie对象
                Cookie[] cookies = request.getCookies();
                for (int i = 0; cookies != null && i < cookies.length; i++) {
                    if ((UserConstant.REMEMBER_LOGIN_INFO).equals(cookies[i].getName())) {
                        cookies[i].setMaxAge(0);
                        response.addCookie(cookies[i]);
                        break;
                    }
                }
            }
            // 判断用户是否选择自动登录
            if (isAutoLogin) { // 选择自动登录
                // 构建保存用户登录信息的Cookie对象
                Cookie loginInfo = new Cookie(UserConstant.AUTO_LOGIN_INFO, phone_email + "&" + password + "&checked");
                loginInfo.setPath(request.getContextPath()); // 这里不设置一下路径发现最后的cookie总是保存后取不出来
                loginInfo.setMaxAge(UserConstant.AUTO_LOGIN_TIME); // 这里是按秒为单位的(设为两周内记住密码)
                response.addCookie(loginInfo);
            }

            // 修改用户的在线状态
            userService.updateOnlineStatus(user.getUserId(), 1);

            info.setFlag(true);
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            // 出现异常，将异常信息添加至info中
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 用户登录
     */
    @RequestMapping("/adminLogin")
    protected void adminLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得表单数据
        String phone_email = request.getParameter("phone_email");
        String password = request.getParameter("password");
        boolean isRememberPwd = request.getParameter("rememberPwd") == null ? false : true;

        // 获得Session对象
        HttpSession session = request.getSession();
        // 构建返回前端数据对象
        ResultInfo info = new ResultInfo();

        try {
            // 进行用户登录操作
            User user = userService.adminLogin(phone_email, MD5Utils.md5(password));

            // 未出现异常，登录成功，在session对象中添加user对象
            session.setAttribute(UserConstant.ADMIN, user);

            // 判断用户是否选择记住密码
            if (isRememberPwd) { // 选择记住密码
                // 构建保存用户登录信息的Cookie对象
                Cookie loginInfo = new Cookie(UserConstant.REMEMBER_LOGIN_INFO, phone_email + "&" + password + "&checked");
                loginInfo.setPath(request.getContextPath()); // 这里不设置一下路径发现最后的cookie总是保存后取不出来
                loginInfo.setMaxAge(UserConstant.REMEMBER_PWD_TIME); // 这里是按秒为单位的(设为两周内记住密码)
                response.addCookie(loginInfo);

            } else { // 未选择记住密码
                // 判断是否存在用户记住密码登录信息的cookie对象
                Cookie[] cookies = request.getCookies();
                for (int i = 0; cookies != null && i < cookies.length; i++) {
                    if ((UserConstant.REMEMBER_LOGIN_INFO).equals(cookies[i].getName())) {
                        cookies[i].setMaxAge(0);
                        response.addCookie(cookies[i]);
                        break;
                    }
                }
            }

            // 修改用户的在线状态
            userService.updateOnlineStatus(user.getUserId(), 1);

            // 将用户权限信息放入session
            session.setAttribute("permissions", user.getPermissions());
            // 将用户权限信息放入session
            session.setAttribute("roles", user.getRoles());

            info.setFlag(true);
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            // 出现异常，将异常信息添加至info中
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 判断手机号是否已被注册
     */
    @RequestMapping("/isPhoneAlreadyRegister")
    protected void isPhoneAlreadyRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得手机号
        String phone = request.getParameter("phone");

        // 构建返回前端数据对象
        ResultInfo info = new ResultInfo();

        try {
            userService.isPhoneAlreadyExist(phone);
            info.setFlag(false);
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            // 出现异常，将异常信息添加至info中
            info.setFlag(true);
            info.setErrorMsg(e.getMessage());
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 判断邮箱是否已被注册
     */
    @RequestMapping("/isEmailAlreadyRegister")
    protected void isEmailAlreadyRegister(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得手机号
        String email = request.getParameter("email");

        // 构建返回前端数据对象
        ResultInfo info = new ResultInfo();

        try {
            userService.isEmailAlreadyExist(email);
            info.setFlag(false);
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            // 出现异常，将异常信息添加至info中
            info.setFlag(true);
            info.setErrorMsg(e.getMessage());
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 退出登录
     */
    @RequestMapping("/exit")
    protected void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(UserConstant.USER);

        try {
            if (user != null) {
                // 修改用户的在线状态
                userService.updateOnlineStatus(user.getUserId(), 0);
            }
        } catch (UserException e) {
            e.printStackTrace();
        }

        // 移除session作用域中的所有值（包括user对象）
        request.getSession().invalidate();

        /* 移除cookies中自动登录的信息 */
        // 判断是否存在用户自动登录信息的cookie对象
        Cookie[] cookies = request.getCookies();

        if (cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if ((UserConstant.AUTO_LOGIN_INFO).equals(c.getName())) {
                    c.setMaxAge(0);
                    c.setPath(request.getContextPath());
                    response.addCookie(c);
                    break;
                }
            }
        }

        // 重定向到首页
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 退出登录
     */
    @RequestMapping("/adminExit")
    protected void adminExit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User)request.getSession().getAttribute(UserConstant.ADMIN);

        try {
            if (user != null) {
                // 修改用户的在线状态
                userService.updateOnlineStatus(user.getUserId(), 0);
            }
        } catch (UserException e) {
            e.printStackTrace();
        }

        // 移除session作用域中的所有值（包括user对象）
        request.getSession().invalidate();

        /* 移除cookies中自动登录的信息 */
        // 判断是否存在用户自动登录信息的cookie对象
        Cookie[] cookies = request.getCookies();

        if (cookies != null && cookies.length > 0) {
            for (Cookie c : cookies) {
                if ((UserConstant.AUTO_LOGIN_INFO).equals(c.getName())) {
                    c.setMaxAge(0);
                    c.setPath(request.getContextPath());
                    response.addCookie(c);
                    break;
                }
            }
        }

        // 重定向到登录页面
        response.sendRedirect(request.getContextPath() + "/admin/index.jsp");
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 修改头像（上传图片）
     */
    @RequestMapping("/changeHeadPic")
    protected void changeHeadPic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得头像路径
        String head_pic_src = request.getParameter("head_pic_src");
        // 构建ResultInfo对象
        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得登录用户的用户对象
        User user = (User) session.getAttribute(UserConstant.USER);
        String savePathUrl = null; // 图片保存在项目中的路径
        String imageUrl = null; // 图片保存在数据库中的路径

        // 设置上传图片的保存路径
        String savePath = request.getServletContext().getRealPath("images/headImg");

        File file = new File(savePath);
        // 判断上传文件的保存目录是否存在
        if (!file.exists() && !file.isDirectory()) {
            // 创建目录
            file.mkdir();
        }

        byte[] bytes = StringUtils.parseBase64ToBytes(head_pic_src);

        /** 字节流转文件 **/
        OutputStream fos = null;
        try {
            // 新文件名（唯一）
            String newFileName = new Date().getTime() + "." + head_pic_src.substring(head_pic_src.indexOf("/") + 1, head_pic_src.indexOf(";"));
            // 设置保存在数据库中的路径
            imageUrl = "images/headImg/" + newFileName;
            savePathUrl = savePath + File.separator + newFileName;
            fos = new FileOutputStream(new File(savePathUrl));

            fos.write(bytes);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            userService.changeHeadPic(user.getUserId(), imageUrl == null ? head_pic_src : imageUrl);
            // 改变存储在session中的用户信息
            user.setHeadImg(head_pic_src);
            session.setAttribute(UserConstant.USER, user);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 修改个性签名
     */
    @RequestMapping("/changeSignature")
    protected void changeSignature(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得个性签名的内容
        String signatureVal = request.getParameter("signature");
        // 构建ResultInfo对象
        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得登录用户的用户对象
        User user = (User) session.getAttribute(UserConstant.USER);

        try {
            userService.changeSignature(user.getUserId(), signatureVal);
            // 改变存储在session中的用户信息
            user.setSignature(signatureVal);
            session.setAttribute(UserConstant.USER, user);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 修改邮箱
     */
    @RequestMapping("/updateEmail")
    protected void updateEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得新邮箱
        String email = request.getParameter("email");
        // 构建ResultInfo对象
        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得登录用户的用户对象
        User user = (User) session.getAttribute(UserConstant.USER);

        try {
            userService.updateEmail(user.getUserId(), email);
            // 改变存储在session中的用户信息
            user.setEmail(email);
            session.setAttribute(UserConstant.USER, user);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 修改手机号
     */
    @RequestMapping("/updatePhone")
    protected void updatePhone(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得新邮箱
        String phone = request.getParameter("phone");
        // 构建ResultInfo对象
        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得登录用户的用户对象
        User user = (User) session.getAttribute(UserConstant.USER);

        try {
            userService.updateEmail(user.getUserId(), phone);
            // 改变存储在session中的用户信息
            user.setPhone(phone);
            session.setAttribute(UserConstant.USER, user);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 删除用户
     */
    @RequestMapping("/deleteUser")
    protected void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得新邮箱
        String userId = request.getParameter("userId");
        // 构建ResultInfo对象
        ResultInfo info = new ResultInfo();

        try {
            // 删除用户
            userService.deleteUser(userId);

            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 个人信息修改表单提交
     */
    @RequestMapping("/updateUserInfo")
    protected void updateUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得表单内容
        String nickName = request.getParameter("nickName");
        String gender = request.getParameter("gender");
        String year = request.getParameter("year");
        String month = request.getParameter("month");
        String day = request.getParameter("day");
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String detail_address = request.getParameter("detail_address");

        if (UserConstant.PROVINCE_SELECT_STR.equals(province)) {
            province = "";
        }
        if (UserConstant.CITY_SELECT_STR.equals(city)) {
            city = "";
        }
        if (month.length() == 1) {
            month = "0" + month;
        }
        if (day.length() == 1) {
            day = "0" + day;
        }
        // 生日
        String birthday = year + "-" + month + "-" + day;
        // 构建ResultInfo对象
        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得登录用户的用户对象
        User user = (User) session.getAttribute(UserConstant.USER);
        // 将参数设置到user中
        user.setNickName(nickName);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setLiveAddress(province + city);
        user.setDetailLiveAddress(detail_address);

        try {
            userService.updateUserInfo(user.getUserId(), user);
            // 改变存储在session中的用户信息
            session.setAttribute(UserConstant.USER, user);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }


    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 找回密码
     */
    @RequestMapping("/retrievePwd")
    protected void retrievePwd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得数据
        String new_pwd = request.getParameter("new_pwd");
        String phone_or_email = request.getParameter("phone_or_email");

        // 构建ResultInfo对象
        ResultInfo info = new ResultInfo();

        try {
            userService.retrievePwd(phone_or_email, new_pwd);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (UserException e) {
            e.printStackTrace();
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }

    }


    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 分页查询所有用户
     */
    @RequestMapping("/findAllUserByPage")
    protected void findAllUserByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得参数
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");

        // 构建RestInfo对象
        ResultInfo info = new ResultInfo();

        // 处理参数
        int currentPage = 0; // 当前页码

        // 先从请求url中找当前页码，如果没有（为null），则到Cookie中去找，如果还没找到，则默认当前页码为1
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        } else {
            // 如果没有传递参数，则默认为第一页
            currentPage = 1;
        }

        int pageSize = 0; // 每页显示条数

        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        } else {
            // 如果没有传递参数，则默认显示10条数据
            pageSize = 10;
        }

        // 构建PageBean对象
        try {
            System.out.println(currentPage);
            PageBean<User> pageBean = userService.findAllByPage(currentPage, pageSize);
            System.out.println(pageBean);
            JSONUtils.writeValueToResponse(pageBean, response);
        } catch (UserException e) {
            e.printStackTrace();
            // 出现异常，将异常信息添加至info中
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            // 将json数据写回客户端
            JSONUtils.writeValueToResponse(info, response);
        }
    }
//    /**
//     * @Description 修改用户密码
//     *
//     * @param request
//     * @param response
//     * @throws ServletException
//     * @throws IOException
//     */
//    protected void updatePassword(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        // 获得用户id
//        String uid = request.getParameter("uid");
//        // 获得用户输入的新密码
//        String newPassword = request.getParameter("new_password");
//
//        try {
//            // 执行更新密码的操作
//            userService.updatePasswordById(new Integer(uid), newPassword);
//            // 修改密码成功，跳转登录界面
//            request.getRequestDispatcher("/login.jsp").forward(request, response);
//        } catch (UserException e) {
//            e.printStackTrace();
//            // 出现异常，将异常信息添加至request作用域中
//            request.setAttribute(UserConstant.UPDATE_PWD_MSG, e.getMessage());
//            // 继续在修改密码界面
//            request.getRequestDispatcher("/update_password.jsp").forward(request, response);
//        }
//    }


}
